@file:Suppress("unused")
package com.meelive.channel.reader

import com.meelive.channel.common.ChannelConstants
import com.meelive.channel.common.V1SchemeUtil
import com.meelive.channel.common.V2SchemeUtil
import java.io.File
import java.lang.Exception

/**
 * Created by leontli on 17/1/19.
 */
object ChannelReader {
    /**
     * get channel value from apk in the v2 signature mode
     *
     * @param channelFile
     * @return
     */
    fun getChannelByV2(channelFile: File): String? {
        println("try to read channel info from apk : " + channelFile.absolutePath)
        return IdValueReader.getStringValueById(channelFile, ChannelConstants.CHANNEL_BLOCK_ID)
    }

    /**
     * get channel info from apk in the v1 signature mode
     *
     * @param channelFile
     * @return
     * @throws Exception
     */
    fun getChannelByV1(channelFile: File): String? {
        try {
            return V1SchemeUtil.readInkeChannel(channelFile)
        } catch (e: Exception) {
            println("APK : " + channelFile.absolutePath + " not have channel info from Zip Comment")
        }
        return null
    }

    /**
     * verify channel info in the v2 signature mode
     *
     * @param file
     * @param channel
     * @return
     */
    fun verifyChannelByV2(file: File, channel: String?): Boolean {
        return if (channel != null) {
            channel == getChannelByV2(file)
        } else false
    }

    /**
     * verify channel info in the v1 signature mode
     *
     * @param file
     * @param channel
     * @return
     */
    fun verifyChannelByV1(file: File, channel: String?): Boolean {
        return if (channel != null) {
            channel == getChannelByV1(file)
        } else false
    }

    /**
     * judge whether apk contain v1 signature
     *
     * @param file
     * @return
     */
    fun containV1Signature(file: File?): Boolean {
        return if (file == null || !file.exists() || !file.isFile) {
            false
        } else V1SchemeUtil.containV1Signature(file)
    }

    /**
     * judge whether apk contain v2 signature block
     *
     * @param file
     * @return
     */
    fun containV2Signature(file: File?): Boolean {
        return if (file == null || !file.exists() || !file.isFile) {
            false
        } else V2SchemeUtil.containV2Signature(file)
    }
}